package com.ld.hpfs.view.excel;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFCellStyle;
import org.apache.poi.hssf.usermodel.HSSFDataFormat;
import org.apache.poi.hssf.usermodel.HSSFFont;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.web.servlet.view.document.AbstractExcelView;

public class MdataGoodsMappingExcelView  extends AbstractExcelView {
	
	@Override
    protected void buildExcelDocument(Map model, HSSFWorkbook workbook, HttpServletRequest request,
            HttpServletResponse response) throws Exception {

        // // 取得模型数据
        // SqlRowSet table = (SqlRowSet) model.get("table");
        // String title = model.get("title").toString();

        // 取得模型数据
    	ArrayList<HashMap<String,Object>> table = (ArrayList<HashMap<String,Object>>) model.get("table");
//        String title = "本地科目表";
    	// 获得Goods的个数
    	int goodsnum = (Integer) table.get(0).get("length");
        // 取得语言环境
    	String language = (String) table.get(0).get("language");
    	ArrayList<HashMap<String,Object>> base = new ArrayList<HashMap<String,Object>>();
    	for(int i = 0;i<goodsnum;i++){
			base.add(table.get(i+1));
    	}
    	
//        // 创建工作表和标题
        HSSFSheet sheet = workbook.createSheet(); // 创建工作区
//        HSSFRow row_title = sheet.createRow(0); // 创建一行引用对象
//        HSSFFont title_font = workbook.createFont(); // 创建标题的字体
//
//        title_font.setFontHeightInPoints((short) 8);
//        title_font.setFontHeight((short) HSSFFont.BOLDWEIGHT_NORMAL);
//        title_font.setColor((short) (HSSFFont.COLOR_RED));
//
//        HSSFCellStyle title_style = workbook.createCellStyle(); // 创建样式
//        title_style.setFont(title_font);
//
//        HSSFCell cell_title = row_title.createCell(1); // 创建单元格引用对象
//        cell_title.setCellStyle(title_style);
//        cell_title.setCellValue(title);

        // 创建数据表头
        String titles[] = new String[8];
        if(language.equals("en")){
        	String titles_en[] = { "Code", "Goods_name"," Tax_denomination","Tax_rate","Specifications","Unit of measurement","Price","Brief_code" };
        	for(int i = 0; i < titles_en.length; i++){
        		titles[i] = titles_en[i];
        	}
        }else if(language.equals("zh_CN")){
        	String titles_cn[] = { "代码", "货物名称","税目","税率","规格","计量单位","单价","简码" };
        	for(int i = 0; i < titles_cn.length; i++){
        		titles[i] = titles_cn[i];
        	}
        }
        HSSFRow row = sheet.createRow(0);
        HSSFCellStyle items_style = workbook.createCellStyle();
        items_style.setAlignment((short) HSSFCellStyle.ALIGN_CENTER);

        HSSFFont celltbnamefont = workbook.createFont();
        celltbnamefont.setFontHeightInPoints((short) 10);

        celltbnamefont.setColor((short) (HSSFFont.COLOR_RED));
        items_style.setFont(celltbnamefont);
        items_style.setWrapText(true);

        for (int i = 0; i < titles.length; i++) {
            HSSFCell cell = row.createCell(i);
            sheet.setColumnWidth(i, 5335);
            cell.setCellValue(titles[i]);
            cell.setCellStyle(items_style);
        }

        HSSFCellStyle datestyle = workbook.createCellStyle();
        HSSFDataFormat df = workbook.createDataFormat();
        datestyle.setDataFormat(df.getFormat("yyyy-mm-dd"));
        // 设置导出文件名
        SimpleDateFormat now = new SimpleDateFormat("yyyyMMddHHmmss");//设置日期格式
        String fileName = "";
        if(language.equals("zh_CN")){
        	fileName = "商品主数据"+"_"+now.format(new Date());
        	fileName = URLEncoder.encode(fileName, "UTF-8");
        }else{
        	fileName = "Goods Mdata"+"_"+now.format(new Date());
        }
        response.setContentType("application/vnd.ms-excel;charset=utf-8"); 
        response.setHeader("Content-disposition","inline;filename="+fileName.toString()+".xls");

        int rowNum = 1;
        for (HashMap<String,Object> entry : base) {
            // create the row data
            HSSFRow datarow = sheet.createRow(rowNum++);
            datarow.createCell(0).setCellValue(entry.get("code").toString());
            datarow.createCell(1).setCellValue(entry.get("name").toString());
            datarow.createCell(2).setCellValue(entry.get("tax_item").toString());
            datarow.createCell(3).setCellValue(entry.get("tax_rate") == null ? "0" : entry.get("tax_rate") == "" ? "0" : entry.get("tax_rate").toString());
            datarow.createCell(4).setCellValue(entry.get("specification").toString());
            datarow.createCell(5).setCellValue(entry.get("measurement").toString());
            datarow.createCell(6).setCellValue(entry.get("default_price") == null ? "0" : entry.get("default_price") == "" ? "0" : entry.get("default_price").toString());
            datarow.createCell(7).setCellValue(entry.get("shortcode").toString());
        }
    }
}
